package microservices.spring_cloud_server.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import microservices.spring_cloud_server.dao.PermissionDao;
import microservices.spring_cloud_server.vo.PermissionVO;

@RestController
public class PermissionService {
	@Autowired
	PermissionDao permissionDao;

	@RequestMapping(value = "/getpermission", method = RequestMethod.POST)
	public List<PermissionVO> getPermission(@RequestBody PermissionVO permission) {
		List<PermissionVO> permissionGroupList = permissionDao.queryPermissionGroupList();
		for (PermissionVO permissionVO : permissionGroupList) {
			PermissionVO qureyVO = new PermissionVO();
			qureyVO.setPermissionGroup(permissionVO.getTitle());
			qureyVO.setRoleId(permission.getRoleId());
			permissionVO.setChildren(permissionDao.queryChildPermissionList(qureyVO));
		}
		return permissionGroupList;
	}

	@RequestMapping(value = "/permissioninsert/{roleId}", method = RequestMethod.POST)
	public void permissionInsert(@PathVariable String roleId, @RequestBody List<PermissionVO> list) {
		PermissionVO permissionVO = new PermissionVO();
		permissionVO.setRoleId(roleId);
		permissionDao.permissionDelete(permissionVO);
		if (list != null && list.size() > 0) {
			permissionDao.permissionInsert(list);
		}
	}

}
